package file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

import main.ErrorException;
import define.Config;
import define.ErrorDefine;

public class RichFile 
{
	private String m_path;
	private String m_name;
	private int m_errorPosition;
	
	public RichFile(String path, int position) throws ErrorException
	{
		m_errorPosition = position;
		setFile(path);
	}
	
	public void setErrorPosition(int position)
	{
		m_errorPosition = position;
	}
	
	public void setFile(String path) throws ErrorException
	{
		int i = path.length() - 1;
		while(i >= 0 && path.charAt(i) != '\\' && path.charAt(i) != '/')
		{
			--i;
		}
		if(i == 0)
		{
			throw new ErrorException(ErrorDefine.E_FILE_PATH_WRONG_ERROR, path, m_errorPosition);
		}
		m_path = path.substring(0, i);
		if(i == path.length() - 1)
		{
			m_name = null;
		}
		else
		{
			m_name = path.substring(i + 1);
		}
	}
	
	public String getWholePath()
	{
		return m_path + "\\" + (m_name == null? "": m_name);
	}
	
	public void copy(RichFile desc, boolean isNeedMkDir) throws ErrorException
	{
		if(desc.m_name == null)
		{
			//if(isNeedMkDir)
			{
				File desDir = new File(desc.getWholePath());
				desDir.mkdirs();
			}
			if(m_name == null)
			{
				File dir = new File(m_path);
				if(!dir.isDirectory())
				{
					throw new ErrorException(ErrorDefine.E_FILE_PATH_WRONG_ERROR, getWholePath(), m_errorPosition);
				}
				for(File temp: dir.listFiles())
				{
					String dirPath = "";
					RichFile newDesc = null;
					if(temp.isDirectory())
					{
						dirPath = temp.getAbsolutePath() + "\\";
						newDesc = new RichFile(desc.m_path + dirPath.substring(m_path.length()), m_errorPosition);
					}
					else 
					{
						dirPath = temp.getAbsolutePath();
						newDesc = desc;
					}
					new RichFile(dirPath, m_errorPosition).copy(newDesc, false);
				}
			}
			else
			{
				RichFile.copyFile(new File(this.getWholePath()), new File(desc.getWholePath() + m_name));
			}
		}
		else
		{
			if(m_name == null)
			{
				throw new ErrorException(ErrorDefine.E_CANT_COPY_DIR_TO_FILE_ERROR, getWholePath(), m_errorPosition);
			}
			RichFile.copyFile(new File(getWholePath()), new File(desc.getWholePath()));
		}
	}
	
	private static void copyFile(File src, File des) throws ErrorException
	{
		FileInputStream fis = null;
		FileChannel inc = null;
		FileOutputStream fos = null;
		FileChannel outc = null;
		try
		{
			fis = new FileInputStream(src);
			inc = fis.getChannel();
			fos = new FileOutputStream(des);
			outc = fos.getChannel();
			long position = inc.position();
			while(position < inc.size())
			{
				long len = Config.COPY_LEN;
				if(len < inc.size() - position)
				{
					len = inc.size() - position;
				}
				inc.transferTo(position, len, outc);
				position += len;
			}
		} 
		catch(Exception e) 
		{
			e.printStackTrace();
			throw new ErrorException(ErrorDefine.E_CANT_COPY_FILE_ERROR, src.getAbsolutePath());
		}
		finally
		{
			try 
			{
				if(inc != null)
				{
					inc.close();
				}
				if(outc != null)
				{
					outc.close();
				}
				if(fis != null)
				{
					fis.close();
				}
				if(fos != null)
				{
					fos.close();
				}
			} 
			catch(Exception e2) 
			{
			}
		}
	}
}
